﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class Holiday : Form
    {
        int curmonth = 0;

        public Holiday()
        {
            InitializeComponent();
            loadHolidays();
            curmonth = mcHoliday.SelectionStart.Month;
        }

        private void loadHolidays()
        {
            int month = mcHoliday.SelectionStart.Month;
            int year = mcHoliday.SelectionStart.Year;

            string sql = "SELECT [date] FROM Holiday WHERE YEAR([date]) = " + year + " AND MONTH([date]) = " + month;
            DataTable dt = Database.getInstance().query(sql);
            DateTime[] bolded = new DateTime[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                bolded[i] = (DateTime)dt.Rows[i][0];
            }
            mcHoliday.BoldedDates = bolded;
        }

        private void mcHoliday_DateChanged(object sender, DateRangeEventArgs e)
        {
            loadHolidays();
        }

        private void btnUbah_Click(object sender, EventArgs e)
        {
            string sql = string.Format("SELECT * FROM Holiday WHERE [date] = '{0}'", mcHoliday.SelectionStart.ToShortDateString());
            DataTable dt = Database.getInstance().query(sql);

            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Tidak ada hari libur pada tanggal ini", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            DataRow row = dt.Rows[0];
            HolidayEdit holidayedit = new HolidayEdit(row[1].ToString());
            DialogResult ans = holidayedit.ShowDialog(this);
            if (ans == DialogResult.OK)
            {
                string keterangan = holidayedit.Keterangan;
                sql = string.Format("UPDATE Holiday SET description = '{0}' WHERE [date] = '{1}'", keterangan, ((DateTime)row[0]).ToShortDateString());
                Database.getInstance().nonquery(sql);
            }
            holidayedit.Close();
        }

        private void btnHapus_Click(object sender, EventArgs e)
        {
            DialogResult ans = MessageBox.Show("Hapus hari libur pada tanggal ini?", "Konfirmasi", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (ans == DialogResult.Cancel) return;

            string sql = string.Format("DELETE FROM Holiday WHERE [date] = '{0}'", mcHoliday.SelectionStart.ToShortDateString());
            Database.getInstance().nonquery(sql);

            loadHolidays();
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            if (txtKeterangan.Text == string.Empty)
            {
                MessageBox.Show("Mohon isi semua input", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            string sql;
            DateTime date = dtpTanggal.Value;

            // Check whether the holiday exists already or not
            if(numJangka.Value > 1)
                sql = string.Format("SELECT [date] FROM Holiday WHERE [date] >= '{0}' AND [date] <= '{1}'", date.ToShortDateString(), date.AddDays((int)numJangka.Value-1).ToShortDateString());
            else
                sql = string.Format("SELECT [date] FROM Holiday WHERE [date] = '{0}'", date.ToShortDateString());
            DataTable dt = Database.getInstance().query(sql);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("Sudah ada hari libur pada tanggal yang dipilih, mohon pilih tanggal yang lain", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            DateTime cur = date;
            for (int i = 0; i < numJangka.Value; i++)
            {
                sql = string.Format("INSERT INTO Holiday VALUES('{0}','{1}')", cur.ToShortDateString(), txtKeterangan.Text);
                Database.getInstance().nonquery(sql);

                cur = cur.AddDays(1);
            }

            loadHolidays();
        }

        private void Holiday_Load(object sender, EventArgs e)
        {

        }
    }
}
